var tree = {
    code: "china",
    name: "中国",
    children: [{
        code: '2973',
        name: '陕西',
        children: [{
            code: '2974',
            name: '西安',
            children: [
                { code: '2975', name: '西安市' },
                { code: '2976', name: '高陵县' }
            ]
        }, {
            code: '2980',
            name: '铜川',
            children: [
                { code: '2981', name: '铜川市' },
                { code: '2982', name: '宜君县' }
            ]
        }, {
            code: '2983',
            name: '宝鸡'
        }]
    }, {
        code: '3371',
        name: '新疆',
        children: [{
            code: '3430',
            name: '巴音郭楞蒙古自治州',
            children: [
                { code: '3431', name: '库尔勒市' },
                { code: '3432', name: '和静县' }
            ]
        }]
    }, {
        code: '3078',
        name: '四川'
    }]
};


function bfs(root) {
    let result = [];
    // root 是个单节点 如果不是就不用了
    let queue = []
    queue.push(root)

    while (queue.length > 0) {
        let node = queue.shift()
        result.push(node)
        if (node.children) {
            for (let i = 0; i < node.children.length; i++) {
                queue.push(node.children[i])
            }
        }
    }
    return result;
}

function dfs(root) {
    let result = []
    if (!root) return result;
    result.push(root)
    let children = root.children
    if (children.length) {
        for (let i = 0; i < children.length; i++) {
            dfs(children[i])
        }
    }
    return result
}


// function bfs(node) {
//     let result = [];
//     while (node.length) { // 注意
//         let item = node.shift();
//         result.push(item)
//         let children = item.children;
//         for (let i = 0; i < children.length; i++) {
//             node.push(children)
//         }
//     }
//     return result;
// }

console.log(bfs(tree))

function dfs(root){
    let result = [];
    if(!root) return result;
    result.push(root);
    let children = root.children;
    if(children){
        for(let i = 0; i< children.length; i++){
            dfs(children[i])
        }
    }

    return result
}

/// 回溯
function back(arr){
    let result = [];
    let temp = [];
    let dep = 0;

    function dfs(temp, dep){
        result.push(temp);
        for(let i =dep; i< arr.length; i++){
            if(i > dep && arr[i] === arr[i-1]) continue
            temp.push(arr[i])
            dfs(temp.slice(),i+1)
            temp.pop()
        }
    }

    dfs(temp,dep)

    return result
}






